package com.jp.fm.service;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.jp.fm.EasyMockParent;
import com.jp.fm.dao.CategoryDao;
import com.jp.fm.dao.TransactionDao;
import com.jp.fm.data.Category;
import com.jp.fm.data.Transaction;

public class CategoryServiceTest extends EasyMockParent {

	private CategoryService categoryService = null;

	private TransactionService mockTransactionService = null;
	private TransactionDao mockTransactionDao = null;
	private CategoryDao mockCategoryDao = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {

		super.setUp();

		categoryService = new CategoryService();
	}

	@After
	public void tearDown() throws Exception {
		super.tearDown();
	}

	@Test
	public final void testGetCategoryByKeyword() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("test1", "val1");

		expect(mockCategoryDao.findAllKeywordToCategoryMappings()).andReturn(
				map);
		expect(mockCategoryDao.findByName("val1")).andReturn(
				new Category(1, "name"));

		replayMocks();

		categoryService.setCategoryDao(mockCategoryDao);
		Integer ret = categoryService.getCategoryByKeyword("test1");

		assertEquals(Integer.valueOf(1), ret);
	}

	@Test
	public final void testGetTopCategoriesOverTimeFrame() {

		categoryService.setCategoryDao(mockCategoryDao);
		categoryService.setTransactionDao(mockTransactionDao);
		categoryService.setTransactionService(mockTransactionService);

		Set<Category> cats = new TreeSet<Category>();
		Category cat = new Category(null, "test1");
		cats.add(cat);

		List<Transaction> list = new ArrayList<Transaction>();
		list.add(new Transaction(null, null, "Visa", null, null, null, null,
				false, null));

		Date startDate = new Date();
		Date endDate = new Date();

		expect(mockCategoryDao.findAll()).andReturn(cats);
		expect(
				mockTransactionDao.findByAny(anyObject(Category.class),
						eq(startDate), eq(endDate), anyObject(String.class),
						anyObject(String.class), anyObject(Boolean.class)))
				.andReturn(list);
		expect(
				mockTransactionService.calculateTransactionsTotal(eq(list),
						anyObject(Category.class))).andReturn(
				Float.valueOf("100.00"));

		replayMocks();

		Map<Float, Category> map = categoryService
				.getTopCategoriesOverTimeFrame(startDate, endDate);

		assertNotNull(map);
		assertEquals(cat, map.get(Float.valueOf("100.00")));
	}
}
